package cn.xuguowen.user.controller;

import cn.hutool.core.collection.CollUtil;
import cn.xuguowen.tool.ExportWordUtil;
import cn.xuguowen.util.GuavaCacheUtil;
import cn.xuguowen.util.RedisShareLockUtil;
import cn.xuguowen.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.function.Function;


/**
 * ClassName: TestController
 * Package: cn.xuguowen.user.controller
 * Description:
 *
 * @Author 徐国文
 * @Create 2023/3/12 11:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class TestController {

    private final RedisUtil redisUtil;

    private final RedisShareLockUtil redisShareLockUtil;

    private final GuavaCacheUtil guavaCacheUtil;


    @GetMapping("testStr")
    public void testStr() {
        // redisTemplate.opsForValue().set("name","xuguowen");

        // 使用封装好的工具类即可
        redisUtil.set("name", "徐国文");
    }

    // 测试redis分布式锁
    @GetMapping("testRedisLock")
    public void testRedisLock() {
        boolean result = redisShareLockUtil.lock("xuguowen", String.valueOf(Thread.currentThread().getId()), 10000L);
        System.out.println(result);
    }

    /**
     * 测试同步日志和异步日志时间上的区别
     * 传统的同步输出日志的效率非常慢，就下面这个栗子测试耗费时间是3698毫秒
     * 异步日志输出日志的效率非常快，就下面这个栗子测试耗费时间是114毫秒
     */
    @GetMapping("testLog")
    public void testLog() {
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            log.info("这是第{}条日志", i);
        }

        long end = System.currentTimeMillis();
        log.info("输出日志耗费时间：{} 毫秒", end - start);
    }

    @GetMapping("testExportWord")
    public void testExportWord() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "徐国文");
        map.put("approveName", "马双");

        // 在工具类中会拼接文件的后缀名的
        ExportWordUtil.exportWord(map, "导出word文档栗子", "exportWord.ftl");
    }


    @GetMapping("testLocalCache")
    public void testLocalCache() {
        List<Long> skuIds = new ArrayList<>();
        // 从本地缓存中查询商品名称的
        Map result1 = guavaCacheUtil.getResult(skuIds, "skuInfo.skuName",
                SkuInfo.class,
                (Function<List, Map>) list -> getSkuName(skuIds));

        // 从本地缓存中查询商品价格的
        Map result2 = guavaCacheUtil.getResult(skuIds, "skuInfo.skuPrice",
                SkuPriceInfo.class,
                (Function<List, Map>) list -> getSkuPrice(skuIds));
    }

    /*
        实际情况下，你可能有两个RPC接口，一个是获取商品名称；一个是获取商品价格信息。这两个接口都需要本地缓存。
        基于guava的本地缓存可以很方便的实现！
     */
    public Map<Long, SkuInfo> getSkuName(List<Long> skuIds) {
        return Collections.EMPTY_MAP;
    }

    public Map<Long, SkuPriceInfo> getSkuPrice(List<Long> skuIds) {
        return Collections.EMPTY_MAP;
    }

    class SkuInfo {
        private Long skuId;
        private String name;
        private Long price;
    }

    class SkuPriceInfo {
        private Long skuId;
        private Long price;
    }

}
